home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / EXAMP602.CPP < prev    next >
C/C++ Source or Header  |  1992-05-12  |  2KB  |  131 lines

  1.     // examp602.cpp - link with binder.obj, mutual.obj,
  2.     // mint.obj, and mstr.obj
  3.     
  4.     #define bfile  "examp602.txt"
  5.  
  6.     #include <fstream.h>
  7.     #include <string.h>
  8.     #include "mint.hpp"
  9.     #include "mstr.hpp"
  10.  
  11.     // display integers and strings
  12.  
  13.     void display(MutuaL M)
  14.     {
  15.         switch (M->ID())  {
  16.         case ID_Mint:
  17.             cout << ((MinT)M)->I() << endl;
  18.             break;
  19.         case ID_Mstr:
  20.             cout << ((MstR)M)->S() << endl;
  21.             break;
  22.         default:
  23.             cout << "unknown" << endl;
  24.             break;
  25.         }
  26.     }
  27.  
  28.  
  29.     // sort integers and strings
  30.  
  31.     int mintstrcmp(MutuaL M1, MutuaL M2)
  32.     {
  33.         // integers sorted to the front
  34.         // strings sorted to the rear
  35.  
  36.         if (M1->ID() == ID_Mint)
  37.             if (M2->ID() == ID_Mint)
  38.                 return ((MinT)M1)->I()
  39.                 - ((MinT)M2)->I();
  40.             else
  41.                 return -1;
  42.         else
  43.             if (M2->ID() == ID_Mint)
  44.                 return 1;
  45.             else
  46.                 return strcmp(
  47.                     ((MstR)M1)->S(),
  48.                     ((MstR)M2)->S());
  49.     }
  50.  
  51.  
  52.     main()
  53.     {
  54.         MBinder::RegisterClass();
  55.         Mint::RegisterClass();
  56.         Mstr::RegisterClass();
  57.  
  58.  
  59.         MBinder b1;
  60.  
  61.         b1.push(new Mstr("Hello LDB!"));
  62.         b1.insQ(new Mstr("Goodbye linked"));
  63.         b1.insQ(new Mstr("list programming!"));
  64.         b1.insQ(b1.rear());
  65.         b1.insQ(new Mstr(
  66.             "Line above tests multilinking!"));
  67.  
  68.         for (int i = 3; i; i--)
  69.             b1.insQ(new Mint(i));
  70.  
  71.         cout << "\n\nMBinder of streamable integers"
  72.             << " and strings!\n\n";
  73.  
  74.         b1.forEach((BDRapplY)display);
  75.  
  76.         cout << "\n\nPress enter to continue ...";
  77.         cin.get();
  78.  
  79.  
  80.         b1.setComP((BDRcomP)mintstrcmp);
  81.  
  82.         Binder::RegisterComP((BDRcomP)mintstrcmp);
  83.  
  84.  
  85.         ofstream os(bfile);
  86.         if (!os)  {
  87.             b1.allDel();
  88.             return 1;
  89.         }
  90.         b1.link();
  91.         os << b1;
  92.         os.close();
  93.  
  94.         b1.allDel();
  95.  
  96.         ifstream is(bfile);
  97.         if (!is)
  98.             return 1;
  99.         MutuaL M;
  100.         is >> M;
  101.         is.close();
  102.         if (!M)
  103.             return 1;
  104.         if (M->ID() != ID_MBinder)
  105.             return 1;
  106.         MBindeR B2 = (MBindeR) M;
  107.  
  108.         cout << "\nStreamed and "
  109.             << "reloaded MBinder with "
  110.             << "multiple links maintained "
  111.             << "\nand sorted with streamed "
  112.             << "compare fnc, ints in front:"
  113.             << " \n";
  114.  
  115.  
  116.         Restream();
  117.  
  118.             // Don't load again from
  119.             // any stream without
  120.             // restreaming StreamRegistry!!!
  121.  
  122.         B2->sort();
  123.  
  124.         B2->forEach((BDRapplY)display);
  125.  
  126.         delete B2;
  127.  
  128.         return 0;
  129.     }
  130.  
  131.